'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	let parmse = {
		...event
	}
	// 便利对象,给为空的查询条件全部删除
	for (let key in parmse) {
		if (!parmse[key]) {
			delete parmse[key]
		} else {
			if (key != '_id' && key != 'queryId') {
				parmse[key] = new RegExp(parmse[key], 'g')
			}
		}
	}
	let dataList = []
	let limit = 100; // 每次查询的数量
	let offset = 0; // 偏移量	
	
	let {
		total
	} = await db.collection('mySkill').where(db.command.or({
		...parmse
	})).count() //获取数据
	do {
		let res = await db.collection('mySkill').where(db.command.or({
			...parmse
		})).orderBy('createTime', 'desc').skip(offset).limit(limit).get();
		dataList = dataList.concat(res.data); // 将查询到的数据添加到列表中	 
		offset += limit; // 更新偏移量
	} while (offset < total);
	
	// 查询子集
	for(let i = 0 ; i < dataList.length ; i++){
		let item = dataList[i]
		let res = await db.collection('mySkillDetail').where({
			queryId:item._id
		}).orderBy('createTime', 'desc').get()
		item.children = res.data || []
	}
	return {
		code: 200,
		message: "操作成功",
		data: dataList,
		total: total,
	}
};
